package cn.tedu.luban.demand.main.repository;

import cn.tedu.luban.demand.main.converter.DemandConverter;
import cn.tedu.luban.demand.main.dao.DemandDAO;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.dos.RequestOrderDO;
import cn.tedu.luban.demand.protocol.param.GrabOrderParam;
import cn.tedu.luban.demand.protocol.query.RequestOrderDBPagerQuery;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DemandRepoImpl implements DemandRepository{
    @Autowired
    private DemandDAO demandDAO;
    @Autowired
    private DemandConverter demandConverter;
    @Override public Integer grabOrder(String requestOrderNo) {
        //抢单 update set grab_status=1 where request_order_no=#{} and grab_status=0
        //还单 update set grab_status=0 where request_order_no=#{} and grab_status=1
        RequestOrderDO rDo=new RequestOrderDO();
        rDo.setRequestOrderNo(requestOrderNo);
        rDo.setGrabStatus(0);
        return demandDAO.updateGrabStatusByOrderNo(rDo);
    }

    @Override
    public Integer returnOrder(String requestOrderNo) {
        RequestOrderDO rDo=new RequestOrderDO();
        rDo.setRequestOrderNo(requestOrderNo);
        rDo.setGrabStatus(1);
        return demandDAO.updateGrabStatusByOrderNo(rDo);
    }

    @Override public Long getRequestOrderCount(RequestOrderQuery query) {
        //1. query转化dbquery
        RequestOrderDBPagerQuery dbQuery = demandConverter.toDbPagerQuery(query);
        //2. 查询总数 直接返回
        return demandDAO.countRequestOrder(dbQuery);
    }

    @Override public List<RequestOrderBO> queryRequestOrders(RequestOrderQuery query) {
        //1. query转化dbquery
        RequestOrderDBPagerQuery dbQuery = demandConverter.toDbPagerQuery(query);
        //2. 查询list分页数据
        List<RequestOrderDO> dos = demandDAO.queryRequestOrders(dbQuery);
        //3. dos转化为bos
        return demandConverter.poList2BoList(dos);
    }

    @Override public Integer grabOrder(GrabOrderParam grabOrderParam) {
        return 0;
    }

    @Override public RequestOrderBO getRequestOrder(String requestOrderNo) {
        return null;
    }

    @Override public Integer returnOrder(GrabOrderParam requestOrderNo) {
        return 0;
    }

    @Override public RequestOrderBO getRequestOrderGrabed(String no) {
        return null;
    }
}
